package com.qdsgjsfk.vision.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsgjsfk.vision.R;

import com.qdsgjsfk.vision.ui.StudentListActivity;
import com.qdsgjsfk.vision.util.NetUtil;
import com.rest.business.RestProxy;
import com.rest.response.GradeClassResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.qdsgjsfk.vision.base.BaseFragment;

/**
 * Created by HP on 2018/12/17.
 */

public class ClassFragment extends BaseFragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.status)
    TextView status;

    String taskId;
    int type;
    List<GradeClassResponse.Grade> list = new ArrayList<>();
    MyAdapter myAdapter;


    public static ClassFragment newInstance(int type, String taskId) {
        Bundle args = new Bundle();
        args.putInt( "type", type );
        args.putString( "taskId", taskId );
        ClassFragment fragment = new ClassFragment();
        fragment.setArguments( args );
        return fragment;
    }


    @Override
    protected void initData() {
        assert getArguments() != null;
        taskId = getArguments().getString( "taskId" );
        type = getArguments().getInt( "type", -1 );
        getAllClass();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_class;
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManage = new LinearLayoutManager( getActivity() );
        recyclerView.setLayoutManager( layoutManage );
        myAdapter = new MyAdapter();
        recyclerView.setAdapter( myAdapter );
        refreshLayout.setRefreshFooter( new BallPulseFooter( mContext ).setSpinnerStyle( SpinnerStyle.Scale ) );
        refreshLayout.setRefreshHeader( new ClassicsHeader( mContext ) );
        refreshLayout.setEnableLoadMore( false );
        refreshLayout.setOnRefreshListener( refreshlayout -> getAllClass() );
        refreshLayout.setOnLoadMoreListener( refreshlayout -> {

        } );
    }


    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            if (viewType == 1) {//1 年纪  0班级
                return new LetterHolder( getLayoutInflater().inflate( R.layout.item_grade, parent, false ) );
            } else if (viewType == 0) {
                return new ClassHolder( getLayoutInflater().inflate( R.layout.item_recyclerview, parent, false ) );
            }
            return null;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            if (holder instanceof LetterHolder) {
                ((LetterHolder) holder).tv_name.setText( list.get( position ).grade );
            }
            if (holder instanceof ClassHolder) {
                GridLayoutManager layoutManage = new GridLayoutManager( getActivity(), 3 );
                ((ClassHolder) holder).recyclerView.setLayoutManager( layoutManage );
                MyItemAdapter myAdapter = new MyItemAdapter( list.get( position ).classArr, list.get( position ).orgId, list.get( position ).prefx, list.get( position ).orgName + " " + list.get( position ).grade, list.get( position ).classYear );
                ((ClassHolder) holder).recyclerView.setAdapter( myAdapter );

//                ((StudentHolder) holder).tv_name.setText(list.get(position).className);
//
//                ((StudentHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Intent mIntent = new Intent(ClassActivity.this, StudentListActivity.class);
//                        mIntent.putExtra("classId", list.get(position).id);
//                        mIntent.putExtra("taskId", taskId);
//                        mIntent.putExtra("gradeName", schoolName + "  " + list.get(position).classYear + "  " + list.get(position).className);
//                        startActivity(mIntent);
//                    }
//                });
            }

        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        @Override
        public int getItemViewType(int position) {
            if (list.get( position ).type == 1)
                return 1;
            else
                return 0;
        }

        class ClassHolder extends RecyclerView.ViewHolder {

            RecyclerView recyclerView;

            ClassHolder(View itemView) {
                super( itemView );
                recyclerView = itemView.findViewById( R.id.recyclerView );
            }

        }


        class LetterHolder extends RecyclerView.ViewHolder {
            TextView tv_name;

            LetterHolder(View itemView) {
                super( itemView );
                tv_name = itemView.findViewById( R.id.tv_name );
            }
        }
    }


    class MyItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        List<GradeClassResponse.Class> list;
        String orgId;
        int prefx;
        String schoolName;
        String classYear;

        MyItemAdapter(List<GradeClassResponse.Class> list, String orgId, int prefx, String schoolName, String classYear) {
            this.list = list;
            this.orgId = orgId;
            this.prefx = prefx;
            this.schoolName = schoolName;
            this.classYear = classYear;
        }


        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ClassItemHolder( getLayoutInflater().inflate( R.layout.item_class, parent, false ) );
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ((ClassItemHolder) holder).tv_name.setText( list.get( position ).className );

            ((ClassItemHolder) holder).itemView.setOnClickListener( v -> {
                Intent mIntent = new Intent( getActivity(), StudentListActivity.class );
                mIntent.putExtra( "classId", list.get( position ).classId );
                mIntent.putExtra( "classYear", classYear );
                mIntent.putExtra( "taskId", taskId );
                mIntent.putExtra( "orgId", orgId );
                mIntent.putExtra( "prefix", prefx );
//                    mIntent.putExtra("gradeName", schoolName + "  " + list.get(position).classYear + "  " + list.get(position).className);
                mIntent.putExtra( "className", schoolName + list.get( position ).className );
                startActivity( mIntent );
            } );
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class ClassItemHolder extends RecyclerView.ViewHolder {
            TextView tv_name;
            ImageView img, img_vision, img_diopter;

            ClassItemHolder(View itemView) {
                super( itemView );
                tv_name = itemView.findViewById( R.id.tv_name );
                img = itemView.findViewById( R.id.img );
                img_vision = itemView.findViewById( R.id.img_vision );
                img_diopter = itemView.findViewById( R.id.img_diopter );

//                examinationDate = itemView.findViewById(R.id.examinationDate);

            }
        }

    }


    private void getAllClass() {
        RestProxy.getInstance().getAllClasses( taskId, type, new Observer<GradeClassResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(GradeClassResponse gradeClassResponse) {
                status.setText( gradeClassResponse.data.statisticsStr2 );
                list.clear();
                for (GradeClassResponse.Grade grade : gradeClassResponse.data.taskClassList.list) {
                    GradeClassResponse gradRes = new GradeClassResponse();
                    GradeClassResponse.Grade gra = new GradeClassResponse.Grade();
                    gra.grade = grade.grade;
                    gra.orgId = grade.orgId;
                    gra.orgName = grade.orgName;
                    gra.type = 1;
                    list.add( gra );
                    GradeClassResponse.Grade cls = new GradeClassResponse.Grade();
                    cls.type = 0;
                    cls.classArr = grade.classArr;
                    cls.orgId = grade.orgId;
                    cls.prefx = grade.prefx;
                    cls.grade = grade.grade;
                    cls.orgName = grade.orgName;
                    cls.classYear = grade.classYear;
                    list.add( cls );
                }
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh();
                NetUtil.onError( e, mContext );
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh();
            }
        } );
    }
}
